﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;

namespace ElingOA6_Admin_DSJCJ.Controllers
{
    /// <summary>
    /// 数据转换临时用 
    /// </summary>
    public class TmpDataConvertController : Controller
    {
        //
        // GET: /TmpDataConvert/

        private BLL.DocCls docClsBLL = new BLL.DocCls();

        /// <summary>
        /// 
        /// </summary>
        private BLL.DocAttach docAttachBLL = new BLL.DocAttach();

        private BLL.Document docmentBLL = new BLL.Document();

        private BLL.TmpDataConvert tmpDataConvertBLL = new BLL.TmpDataConvert();
        /// <summary>
        /// 生成缩率图
        /// </summary>
        /// <returns></returns>
        public void GenThumbPic()
        {
             string entityNo = GlobalConstant.ENTITYNO;
            Model.Document newDocumentModel = new Model.Document();
            Model.DocCls docClsModel = docClsBLL.GetModel(entityNo, "02");
            string errorMessage = "";
            int total=0;
            List<Model.Document> docList = docmentBLL.GetListByPage("t_document", "*", 10000, 0, " FThumbGenType IS NULL AND FDocClsCd='02'", "FDocId", 0, out total);
            List<Model.Document> docConvertList = new List<Model.Document>();
            try
            {
                foreach (Model.Document docModel in docList)
                {
                    Model.Document tmpDocModel = docModel;
                    List<Model.DocAttach> docAttachPicList = docAttachBLL.GetDocAttachListByDocID(docModel.FDocId, "PIC");
                    UploadBreviaryImg(ref tmpDocModel, docClsModel, docAttachPicList);

                    if (tmpDocModel.FThumbSeqID.HasValue)
                    {
                        docConvertList.Add(tmpDocModel);
                    }
                }
                tmpDataConvertBLL.UpdateDocumentThumb(docConvertList);
            }
            catch(Exception ee)
            {
                errorMessage = ee.Message;
            }
            if (errorMessage == "")
            {
                Response.Write("成功");
            }
            else
            {
                Response.Write("错误:" + errorMessage);
            }
            Response.Flush();
            Response.End();
        }

        /// <summary>
        /// 上传缩略图，并取得相关数据
        /// </summary>
        /// <param name="newDocumentModel">新的文档对象</param>
        /// <param name="docClsModel">信息类别对象</param>
        /// <param name="documentModel">提交的文档对象</param>
        private void UploadBreviaryImg(ref Model.Document newDocumentModel, Model.DocCls docClsModel, List<Model.DocAttach> docAttachPicList)
        {
            string entityNo = GlobalConstant.ENTITYNO;
            int imgWidth = docClsModel.FPicWidth; //缩图图宽
            int imgHeight = docClsModel.FPicHeight;//缩略图高
            int isWater = (int)docClsModel.FIsPicWaterMark;//是否加水印

            int WaterMarkCls = int.Parse(docClsModel.FWaterMarkCls);//水印类型
            string strWaterMarkPos = docClsModel.FWaterMarkPos;//水印位置
            int LeftRightMargin = docClsModel.FLeftRightMargin;//水印左右边距
            int TopBottomMargin = docClsModel.FTopBottomMargin;//水印上下边距
            //缩略图存放路径
            string docClsPath = docClsBLL.GetDocClsCdS(docClsModel.FDocClsCd, entityNo);//获取当前类别和其父类别组成的路径
            string strBreviaryImgPath = Url.Content("~/Publish/" + entityNo + "/DOC/" + docClsPath + "/" + DateTime.Now.Year + "/" + DateTime.Now.Month.ToString().PadLeft(2, '0'));
            //路径不存在就创建文件夹
            GenerateUtil.addfolder(strBreviaryImgPath);
            string strImgName = GenerateUtil.GenerateStringId() + ".jpg";//生成随机的缩略图名称，缩略图都保存为jpg格式
            strBreviaryImgPath += "/" + strImgName;//组合完整的缩略图路径
            newDocumentModel.FThumbFileExt = "image/pjpeg";
            newDocumentModel.FThumbSrvFileName = strBreviaryImgPath;//缩略图服务器路径
            newDocumentModel.FThumbGenType ="02";//缩略图产生方式

            if (docAttachPicList.Count > 0)
            {
                //有上传图片，则提取上传图片的第一张图片
                string svrFileName = docAttachPicList[0].FAttachSvrFileName;
                if (!GenerateUtil.ExistsFile(svrFileName))
                {
                    //文件不存在,判断是否是加了虚拟目录名的关系，是的话就去掉虚拟目录名称
                    svrFileName = GenerateUtil.GetPath(svrFileName);
                }
                if (GenerateUtil.ExistsFile(svrFileName))//有图片地址
                {
                    //生成缩略图
                    BreviaryImg.SetImg(svrFileName, strBreviaryImgPath, imgWidth, imgHeight, int.Parse(newDocumentModel.FThumbGenType), isWater, WaterMarkCls, newDocumentModel.FEntityNo, strWaterMarkPos, LeftRightMargin, TopBottomMargin);

                    newDocumentModel.FThumbSeqID =1;//序号      
                    //获取图片二进制流
                    newDocumentModel.FThumbContent = UploadControlHelper.GetImgByte(strBreviaryImgPath);
                    newDocumentModel.FThumbOldFileName = UploadControlHelper.GetLoadImageName(svrFileName);//获取图片原名
                }
            }
           
        }

    }
}
